/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dale2507.gates.commands;

import dale2507.gates.GateDirectory;
import dale2507.gates.Messenger;
import dale2507.gates.data.permissions.Permissions;
import dale2507.gates.exceptions.GateConnectionException;
import dale2507.gates.exceptions.GateException;
import dale2507.gates.exceptions.PermissionException;
import dale2507.gates.gate.Address;
import dale2507.gates.gate.AddressItem;
import dale2507.gates.gate.Gate;
import dale2507.gates.gate.comparator.GateNameComparator;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

/**
 *
 * @author Dale
 */
public class GateCommand implements CommandExecutor {

    /**
     * Executes the gate command
     * @param cs Source of the command
     * @param cmnd Command which was executed
     * @param string Alias of the command which was used
     * @param strings Passed command arguments
     * @return true if a valid command, otherwise false
     */
    @Override
    public boolean onCommand(CommandSender cs, Command cmnd, String string, String[] strings) {
        if (strings.length > 1 && strings[0].equalsIgnoreCase("info")) {
            return info(cs, cmnd, string, strings);
        } else if (strings.length > 1 && strings[0].equalsIgnoreCase("activate")) {
            return activate(cs, cmnd, string, strings);
        } else if (strings.length > 1 && strings[0].equalsIgnoreCase("deactivate")) {
            return deactivate(cs, cmnd, string, strings);
        } else if (strings.length > 1 && strings[0].equalsIgnoreCase("enable")) {
            return enable(cs, cmnd, string, strings);
        } else if (strings.length > 1 && strings[0].equalsIgnoreCase("disable")) {
            return disable(cs, cmnd, string, strings);
        } else {
            return false;
        }
    }
    
    private boolean info(CommandSender cs, Command cmnd, String string, String[] strings) {
        try {
            if (cs instanceof Player &&
                    !Permissions.getPermissions().getCommandPermissions().allowGateInfo((Player) cs)) {
                throw new PermissionException("You are not allowed to use that command");
            }
        
            Gate g = GateDirectory.getInstance().getGate(new GateNameComparator(), strings[1]);
            if (g != null) {
                Messenger.sendMessage(cs, Messenger.INFO, "Gate Name: " + g.getSettings().getName());
                Messenger.sendMessage(cs, Messenger.INFO, "Address:");
                Address a = g.getSettings().getAddress();
                if (a != null) {
                    for (int x=0; x<9; x++) {
                        AddressItem ai = a.getAddressItem(x);
                        if (ai != null) Messenger.sendMessage(cs, Messenger.INFO, "    " + (x+1) + ": " + ai);
                    }
                } else {
                    Messenger.sendMessage(cs, Messenger.INFO, "Outgoing only");
                }

                Messenger.sendMessage(cs, Messenger.INFO, "Auto Dial Address:");
                a = g.getSettings().getAutoDialAddress();
                if (a != null) {
                    for (int x=0; x<9; x++) {
                        AddressItem ai = a.getAddressItem(x);
                        if (ai != null) Messenger.sendMessage(cs, Messenger.INFO, "    " + (x+1) + ": " + ai);
                    }
                } else {
                    Messenger.sendMessage(cs, Messenger.INFO, "No auto dial");
                }
                Messenger.sendMessage(cs, Messenger.INFO, "Gate Status: " + g.getStatus().toString());
            } else {
                Messenger.sendMessage(cs, Messenger.ERROR, "Gate does not exist");
            }

            return true;
        } catch (PermissionException ex) {
            Messenger.sendMessage(cs, Messenger.ERROR, ex.getMessage());
            return true;
        }
    }
    
    private boolean activate(CommandSender cs, Command cmnd, String string, String[] strings) {
        try {
            if (cs instanceof Player &&
                    !Permissions.getPermissions().getCommandPermissions().allowGateActivate((Player) cs)) {
                throw new PermissionException("You are not allowed to use that command");
            }
        
            Gate source = GateDirectory.getInstance().getGate(new GateNameComparator(), strings[1]);
            if (source == null) {
                Messenger.sendMessage(cs, Messenger.ERROR, "Source gate does not exist.");
                return true;
            }

            if (strings.length > 2) {
                Gate dest = GateDirectory.getInstance().getGate(new GateNameComparator(), strings[2]);
                if (dest == null) {
                    Messenger.sendMessage(cs, Messenger.ERROR, "Destination gate does not exist.");
                    return true;
                } else if (dest.getSettings().getAddress() == null || dest.getSettings().getAddress().isEmpty()) {
                    Messenger.sendMessage(cs, Messenger.ERROR, "Destination gate is outgoing only.");
                    return true;
                }

                try {
                    source.getActivator().activate(cs, dest.getSettings().getAddress());
                } catch (PermissionException | GateConnectionException ex) {
                    Messenger.sendMessage(cs, Messenger.ERROR, ex.getMessage());
                }
            } else {
                try {
                    source.getActivator().activate(cs);
                } catch (PermissionException | GateConnectionException ex) {
                    Messenger.sendMessage(cs, Messenger.ERROR, ex.getMessage());
                }
            }

            return true;
        } catch (PermissionException ex) {
            Messenger.sendMessage(cs, Messenger.ERROR, ex.getMessage());
            return true;
        }
    }
    
    private boolean deactivate(CommandSender cs, Command cmnd, String string, String[] strings) {
        try {
            if (cs instanceof Player &&
                    !Permissions.getPermissions().getCommandPermissions().allowGateDeactivate((Player) cs)) {
                throw new PermissionException("You are not allowed to use that command");
            }
        
            Gate source = GateDirectory.getInstance().getGate(new GateNameComparator(), strings[1]);
            if (source == null) {
                Messenger.sendMessage(cs, Messenger.ERROR, "Source gate does not exist.");
            }

            try {
                source.getActivator().deactivate(cs);
            } catch (PermissionException ex) {
                Messenger.sendMessage(cs, Messenger.ERROR, ex.getMessage());
            }
            return true;
        } catch (PermissionException ex) {
            Messenger.sendMessage(cs, Messenger.ERROR, ex.getMessage());
            return true;
        }
    }

    private boolean enable(CommandSender cs, Command cmnd, String string, String[] strings) {
        try {
            if (cs instanceof Player &&
                    !Permissions.getPermissions().getCommandPermissions().allowGateDisable((Player) cs)) {
                throw new PermissionException("You are not allowed to use that command");
            }
        
            Gate g = GateDirectory.getInstance().getGate(new GateNameComparator(), strings[1]);
            if (g == null) {
                Messenger.sendMessage(cs, Messenger.ERROR, "Gate does not exist.");
                return true;
            }

            try {
                g.setEnabled(true);
                Messenger.sendMessage(cs, Messenger.SUCCESS, g.getSettings().getName() + " is enabled");
            } catch (GateException ex) {
                Messenger.sendMessage(cs, Messenger.ERROR, ex.getMessage());
            }
            return true;
        } catch (PermissionException ex) {
            Messenger.sendMessage(cs, Messenger.ERROR, ex.getMessage());
            return true;
        }
    }

    private boolean disable(CommandSender cs, Command cmnd, String string, String[] strings) {
        try {
            if (cs instanceof Player &&
                    !Permissions.getPermissions().getCommandPermissions().allowGateDisable((Player) cs)) {
                throw new PermissionException("You are not allowed to use that command");
            }
        
            Gate g = GateDirectory.getInstance().getGate(new GateNameComparator(), strings[1]);
            if (g == null) {
                Messenger.sendMessage(cs, Messenger.ERROR, "Gate does not exist.");
                return true;
            }

            try {
                g.setEnabled(false);
                Messenger.sendMessage(cs, Messenger.SUCCESS, g.getSettings().getName() + " is disabled");
            } catch (GateException ex) {
                Messenger.sendMessage(cs, Messenger.ERROR, ex.getMessage());
            }
            return true;
        } catch (PermissionException ex) {
            Messenger.sendMessage(cs, Messenger.ERROR, ex.getMessage());
            return true;
        }
    }
    
}